package com.boyue.dao.impl;

import com.boyue.dao.AddressListDao;
import com.boyue.dao.CommonDao;
import com.boyue.entity.AddressList;
import com.boyue.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

/**
 * @auth wzy
 * @date 2023/9/1 14:48
 */
public class AddressListDaoImpl extends CommonDao implements AddressListDao {


    @Override
    public List<AddressList> list() throws SQLException {
        String sql = "SELECT `addId`, `addName`, `addPhone`, `addMail`, `addRelation` FROM `addresslist`";
        return super.executeQuery(AddressList.class, sql, null);
    }

    @Override
    public PageInfo<AddressList> listPage(PageInfo pageInfo,AddressList addressList) throws SQLException {

        String sql = "SELECT `addId`, `addName`, `addPhone`, `addMail`, `addRelation` FROM `addresslist`";

        String sqlCondition = " where 1=1 ";
        if (addressList.getAddName()!=null && addressList.getAddName().length()>0){
            sqlCondition += " and addName like '%"+addressList.getAddName()+"%' ";
        }

        if (addressList.getAddPhone()!=null && addressList.getAddPhone().length()>0){
            sqlCondition += " and addPhone like '%"+addressList.getAddPhone()+"%' ";
        }

        if (addressList.getAddRelation()!=null && addressList.getAddRelation().length()>0){
            sqlCondition += " and addRelation like '%"+addressList.getAddRelation()+"%' ";
        }


        sql = sql + sqlCondition + " limit ?,?";

        int beginIndex = (pageInfo.getPageIndex()-1)*pageInfo.getPageSize();

        List<AddressList> addressLists = super.executeQuery(AddressList.class, sql, new Object[]{beginIndex, pageInfo.getPageSize()});
        pageInfo.setPageList(addressLists);

        String sqlCount = "SELECT count(1) FROM  addresslist "+sqlCondition;
        Integer count = super.executeQuery(Integer.class, sqlCount, null).get(0);
        pageInfo.setCount(count);

        return pageInfo;
    }

    @Override
    public AddressList getById(Integer id) throws SQLException {
        String sql = "SELECT `addId`, `addName`, `addPhone`, `addMail`, `addRelation` FROM `addresslist` where addId=?";
        List<AddressList> addressLists = super.executeQuery(AddressList.class, sql, new Integer[]{id});
        if (addressLists.size()>0){
            return addressLists.get(0);
        }
        return null;
    }

    @Override
    public Integer add(AddressList addressList) throws SQLException {
        String sql = "INSERT INTO `addresslist` (`addName`, `addPhone`, `addMail`, `addRelation`,addPhoto) VALUES (?,?,?,?,?)";
        return super.executeUpdate(sql,new Object[]{addressList.getAddName(), addressList.getAddPhone(), addressList.getAddMail(), addressList.getAddRelation(), addressList.getAddPhone()});
    }

    @Override
    public Integer delete(Integer addId) throws SQLException {
        String sql = "delete from `addresslist` where addId=?";
        return super.executeUpdate(sql,new Object[]{addId});
    }
}
